package com.cheung.leetcode.hash;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @BelongsProject : java-leetcode
 * @BelongsPackage : com.cheung.leetcode.hash
 * @Author :  cheungming
 * @CreateTime : 2024-05-04 12:58:05
 * @Description : 350类似349题
 * @Version : 1.0
 */
public class Code350Test {

    public int[] intersect(int[] nums1, int[] nums2) {
        int[] n1 = new int[1001];
        int[] n2 = new int[1001];

        for (int i : nums1) {
            n1[i]++;
        }

        for (int i : nums2) {
            n2[i]++;
        }

        List<Integer> list = new ArrayList<>();

        for (int i = 0; i < 1001; i++) {
            if (n1[i] > 0 && n2[i] > 0) {
                int min = Math.min(n1[i], n2[i]);
                for (int j = 0; j < min; j++) {
                    list.add(i);
                }


            }
        }

        return list.stream().mapToInt(Integer::intValue).toArray();

    }


    @Test
    public void test1() {
        int[] nums1 = {1, 2, 2, 1}, nums2 = {2, 2};
        assert Arrays.equals(intersect(nums1, nums2), new int[]{2, 2});
    }

    @Test
    public void test2() {
        int[] num1 = {4, 9, 5}, nums2 = {9, 4, 9, 8, 4};
        assert Arrays.equals(intersect(nums2, num1), new int[]{4, 9});
    }
}


